package com.scjp.chapter3.stack_heap_example;

public class Dog extends Collar {

	/*
	 * Instance variables.
	 */
	static Collar collar;
	String name;

	public Dog(String text) {
		super(text);
		System.out.println(text);
	}

	public static void main(String[] args) {
		Dog dog; // local variable
		dog = new Dog("");
		dog.go(dog);
		System.out.println(dog.getName());
		dog.setName(("Bobby").toUpperCase());
		System.out.println(dog.getName());

		int octal = 040;
		System.out.println("octal = " + octal);

		float g = 49837849.029847F;
		System.out.println(g);

		char unicode = '\u0012';
		System.out.println("\n Unicode : " + unicode + "\n");

		printHexaNumbers();

		compareStringsExample();

		byteSum();

		explicitCast();
		longToByteCast();
		castFloatExample();

		collar = new Collar();
		System.out.println("\n Collar stuff : " + collar.val + " / "
				+ collar.string);

		collar.val = 100;
		collar.string = "bye";
		System.out.println("\n Collar stuff changed : " + collar.val + " / "
				+ collar.string);

		Dog[] vector = new Dog[3];
		System.out.println(vector.length);

		for (int i = 0; i < vector.length; i++) {
			vector[i] = new Dog("Sony");
			System.out.println("vector = " + vector[i]);
		}

		Dog[][] dogs = { { new Dog("Fifi"), new Dog("Fluffy") },
				{ new Dog("Bali"), new Dog("Simi"), new Dog("Mani") } };
		for (int i = 0; i < dogs.length; i++) {
			System.out.println("Dog[" + i + "][" + i + "] = " + dogs[i]);
		}
		
		go3();
		checkValues();
	}

	private static void compareStringsExample() {
		String s1 = new String("AaA");
		String s2 = new String("AaA");
		appendStrings(s1, s2);

		System.out.println("s1 == s2? : " + (s1 == s2));
		System.out.println("s1 equals s2? : " + s1.equals(s2) + "\n");

		String s3 = "LaL";
		String s4 = new String("LaL");
		// appendStrings(s3, s4);

		System.out.println("s3 == s4? : " + (s3 == s4));
		System.out.println("s3 equals s4? : " + s3.equals(s4));
	}

	void go(Dog dog) {
		// Local variable : dog
		collar = new Collar();
		dog.setName(("Puppy").toLowerCase());
	}

	void setName(String newName) {
		// Local variable : newName
		name = newName;
	}

	String getName() {
		return name;
	}

	static void printHexaNumbers() {
		int x = 0X0001;
		int y = 0x7fffffff; // represented as Decimal
		int z = 0xDeadCafe; // represented as DWord
		System.out.println("x = " + x + " y = " + y + " z = " + z);
	}

	static void appendStrings(String s1, String s2) {
		StringBuilder result = new StringBuilder();
		result = result.append(s1).append(s2);
		System.out.println(result);
	}

	static void byteSum() {
		byte a = 4;
		byte b = 8;
		byte c = (byte) (a + b);
		System.out.println("\nByte sum is : " + c);
	}

	static void explicitCast() {
		// float x = (int)3957.229; // legal cast
		int x = (int) 3957.229; // legal cast
		System.out.println("Explicit cast : int x = " + x);
	}

	static void longToByteCast() {
		long l = 133L;
		byte b = (byte) l;
		System.out.println("The byte cast is " + b);
	}

	static void castFloatExample() {
		float value = 12.55559f;
		short s = (short) value;
		System.out.println("The short value of float " + value + " is : " + s);
	}

	static void go3() {
		for (int z = 0; z < 5; z++) {
			boolean test = false;
			if (z == 3) {
				test = true;
				//break;
			}
			System.out.print(test + "\n"); 
		}
	}
	
	static void checkValues() {
		int i = 10;
		int n = i++%5;
		System.out.println("i = " + i + " ; n = " + n);
	}

}
